package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONArray;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PagingUtil {

	public static Map<Object, Object> pagingQuery(Long page, Long limit,
			List<Map<Object, Object>> list, int total) {
		Map<Object, Object> returnMap = new HashMap<>();
		returnMap.put("data", list);

		// pagination对象
		HashMap<String, Object> pagination = new HashMap<>();
		pagination.put("limit", limit);
		pagination.put("page", page);
		pagination.put("total", total);
		if (total >= page * limit) {
			pagination.put("has_next", true);
		} else {
			pagination.put("has_next", false);
		}

		if (1 <= (page - 1) * limit + 1) {
			pagination.put("has_prev", true);
		} else {
			pagination.put("has_prev", false);
		}
		returnMap.put("pagination", pagination);

		if (list.size() > 0) {
			returnMap.put("error_code", 0);
			returnMap.put("message", "操作成功");
		} else {
			returnMap.put("error_code", 0);
			returnMap.put("message", "数据不存在");
		}

		returnMap.put("success", true);
		return returnMap;
	}

	public static Map<Object, Object> pagingQuery2(int page, int limit,
			List<Map<String, Object>> list, int total) {
		Map<Object, Object> returnMap = new HashMap<>();
		returnMap.put("data", list);

		// pagination对象
		HashMap<String, Object> pagination = new HashMap<>();
		pagination.put("limit", limit);
		pagination.put("page", page);
		pagination.put("total", total);
		if (total >= page * limit) {
			pagination.put("has_next", true);
		} else {
			pagination.put("has_next", false);
		}

		if (1 <= (page - 1) * limit + 1) {
			pagination.put("has_prev", false);
		} else {
			pagination.put("has_prev", true);
		}
		returnMap.put("pagination", pagination);

		if (list.size() > 0) {
			returnMap.put("error_code", 0);
			returnMap.put("message", "操作成功");
		} else {
			returnMap.put("error_code", 0);
			returnMap.put("message", "数据不存在");
		}

		returnMap.put("success", true);
		return returnMap;
	}

	public static Map<Object, Object> pagingQueryJsonArray(Long page, Long limit, JSONArray list, int total) {
		Map<Object, Object> returnMap = new HashMap<>();
		returnMap.put("data", list);

		// pagination对象
		HashMap<String, Object> pagination = new HashMap<>();
		pagination.put("limit", limit);
		pagination.put("page", page);
		pagination.put("total", total);
		if (total >= page * limit) {
			pagination.put("has_next", true);
		} else {
			pagination.put("has_next", false);
		}

		if (1 <= (page - 1) * limit + 1) {
			pagination.put("has_prev", false);
		} else {
			pagination.put("has_prev", true);
		}
		returnMap.put("pagination", pagination);

		if (list.size() > 0) {
			returnMap.put("error_code", 0);
			returnMap.put("message", "操作成功");
		} else {
			returnMap.put("error_code", 0);
			returnMap.put("message", "数据不存在");
		}

		returnMap.put("success", true);
		return returnMap;
	}

	public static Map<Object, Object> pagingQueryList(Long page, Long limit,  int total) {
		Map<Object, Object> returnMap = new HashMap<>();

		// pagination对象
		HashMap<String, Object> pagination = new HashMap<>();
		pagination.put("limit", limit);
		pagination.put("page", page);
		pagination.put("total", total);
		if (total >= page * limit) {
			pagination.put("has_next", true);
		} else {
			pagination.put("has_next", false);
		}

		if (1 <= (page - 1) * limit + 1) {
			pagination.put("has_prev", false);
		} else {
			pagination.put("has_prev", true);
		}

		if (total > 0) {
			returnMap.put("error_code", 0);
			returnMap.put("message", "查询成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("error_code", 0);
			returnMap.put("success", true);
			returnMap.put("message", "数据不存在");
		}

		returnMap.put("pagination", pagination);
		return returnMap;
	}
}
